package com.cloudbroker.bcs.platform.ltb.middle.util;

import com.cloudbroker.bcs.platform.ltb.common.util.CacheHelper;
import com.cloudbroker.bcs.platform.ltb.common.util.SessionHelper;
import com.cloudbroker.bcs.platform.ltb.middle.entity.Market;
import org.apache.commons.lang.StringUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by daibd19671 on 2016/8/20.
 */
public class AskCacheUtil {

    private static CacheHelper cacheHelper = CacheHelper.getInstance();
    public static final String ASK_NUM = "ask_num.";
    /**
     * 获取已提问次数
     * */
    public static int getAskNum(long user_id){
        String num = cacheHelper.get(ASK_NUM+user_id);
        if(StringUtils.isEmpty(num)){
            return 0;
        } else {
            return Integer.valueOf(num).intValue();
        }
    }

    /**
     * 增加提问次数
     * */
    public static void addAskNum(long user_id){
        int num = AskCacheUtil.getAskNum(user_id) + 1;
        long time = cacheHelper.ttl(ASK_NUM+user_id);
        if(time < 0){
            Calendar todayEnd = Calendar.getInstance();
            todayEnd.set(Calendar.HOUR_OF_DAY, 23);
            todayEnd.set(Calendar.MINUTE, 59);
            todayEnd.set(Calendar.SECOND, 59);
            todayEnd.set(Calendar.MILLISECOND, 999);
            time = (todayEnd.getTime().getTime() - new Date().getTime())/1000;
        }
        cacheHelper.set(ASK_NUM+user_id, String.valueOf(num),Long.valueOf(time).intValue());
    }

}
